import {
  View,
  Text,
  Image,
  TouchableWithoutFeedback,
  TouchableOpacity,
} from "react-native";
import { R } from '../../../core';
import {IconFont } from 'enoad-common/src/index';
import { EnoadBaseModule } from "../../index";
import { v4 as uuidv4 } from "uuid";
import React from "react";
import _, { parseInt } from "lodash";
const responseLayout = require("./layout.json");
const responseData = require("./data.json");

export default class VisitData extends EnoadBaseModule {
  constructor(config: string) {
    super(config);
  }

  /**
   * 获取的布局信息
   * @returns
   */
  getLayout() {
    if (responseLayout && responseLayout.scode == "0") {
      if (responseLayout.body && responseLayout.body.dataListViews) {
        return responseLayout.body.dataListViews;
      } else {
        return null;
      }
    }
    return null;
  }

  /**
   * 获取的数据信息
   * @returns
   */
  getData() {
    let data = { showDatas: "", originData: "" };
    if (responseData && responseData.scode == "0") {
      if (responseData.body) {
        data.showDatas = responseData.body.showDatas;
        data.originData = responseData.body.originData;
      }
    }
    return data;
  }

  getURL() {
    return "";
  }

  /**
   * 获取每一行的布局和操作信息
   * @param componentId 唯一识别
   * @param item 数据
   * @param showItems 更多布局
   * @param actions 操作
   * @returns
   */
  getDataWithLayout(
    componentId: string,
    item: any,
    showItems: any,
    actions: any,
    openDetail: any,
    addCart:any
  ) {
    //先展示图片字段
    let image;
    let other = new Array();
    _.forEach(showItems.showItems, (itemLayout: any) => {
      if (itemLayout.itemType == 29) {
        image = this.getItemView(item, itemLayout);
      } else {
        // if (other.length < 3) {
        other.push(this.getItemView(item, itemLayout));
        // }
      }
    });

    return (
      <TouchableOpacity
        key={uuidv4()}
        style={[R.styles.schadule_bord, { flexDirection: "row" }]}
        onPress={() => {
          openDetail && openDetail(componentId, item);
        }}
      >
        {image}
        <View style={{ flex: 1, backgroundColor: R.color.white }}>
          <Text
            numberOfLines={2}
            style={{
              color: "#182437",
              fontSize: 14,
              fontFamily: "PingFangSC-Regular",
              paddingBottom: 2,
            }}
          >
            {item.productName}
          </Text>
          {other}
          <View style={{ flexDirection: "row", alignItems: "center" }}>
            <View style={{ flexDirection: "column", flex: 1 }}>
              <Text
                style={{
                  color: "#FF7D00",
                  fontSize: 14,
                  fontFamily: "Helvetica-Bold",
                }}
              >
                {`￥ ${item.priceUnit}`}
              </Text>
              <Text
                style={{
                  color: "#A1A5AF",
                  fontSize: 10,
                  textDecorationLine: "line-through",
                }}
              >
                {`￥ ${item.originPrice}`}
              </Text>
            </View>
            <TouchableOpacity
              style={{ paddingLeft: 10, paddingRight: 10 }}
              onPress={() => {
                addCart && addCart(item);
              }}
            >
              <IconFont name={"jiahao"} size={24} color={"#4E80F5"} />
            </TouchableOpacity>
          </View>
        </View>
      </TouchableOpacity>
    );
  }

  getItemView(data: any, itemLayout: any) {
    if (itemLayout.itemType == 29) {
      //图片字段；
      let url = data[`${itemLayout.apiKey}`];
      if (url) {
        let urlJson = JSON.parse(url);
        if (urlJson && urlJson.length > 0) {
          url = urlJson[0];
        }
        if (url) {
          return (
            <View key={data.id + uuidv4()}>
              <Image
                style={{ height: 130, width: 130, marginRight: 10 }}
                resizeMode={"cover"}
                source={{ uri: url }}
              />
            </View>
          );
        }
      }
    } else {
      return (
        <View key={data.id + uuidv4()} style={{ flexDirection: "column" }}>
          <Text
            numberOfLines={1}
            style={{
              color: "#A1A5AF",
              fontSize: 12,
              fontFamily: "PingFangSC-Regular",
            }}
          >
            {itemLayout.label}
          </Text>
          <Text
            numberOfLines={1}
            style={{
              color: "#182437",
              fontSize: 14,
              fontFamily: "PingFangSC-Regular",
              paddingBottom: 2,
            }}
          >
            {data[`${itemLayout.apiKey}`]}
          </Text>
        </View>
      );
    }
  }
}
